<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>管理员列表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
  <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
  <link rel="stylesheet" href="/css/font.css">
  <link rel="stylesheet" href="/css/xadmin.css">
  <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
  <script type="text/javascript" src="/js/xadmin.js"></script>
  <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
  <!--[if lt IE 9]>
  <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
  <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
  <![endif]-->
</head>

<body>


<form class="layui-form layui-col-md12 x-so" id="form">
  <!--      <div class="layui-form-item">-->
  <!--        <input type="checkbox" name="roles" value="1" id="start"/>前端-->
  <!--      </div>-->

  <!--      <div class="layui-form-item">-->
  <!--        <input type="checkbox" name="roles" value="2" id="end"/>后端-->
  <!--      </div>-->

  <!--      <div class="layui-form-item">-->
  <!--        <input type="checkbox" name="roles" value="3" id="middle"/>美工-->
  <!--      </div>-->

  <!--      <div class="layui-form-item">-->
  <!--        <button class="layui-btn"  lay-submit="" lay-filter="formDemo" id="search"><i class="layui-icon">&#xe615;</i></button>-->
  <!--      </div>-->

</form>

<script>

  let adminName = "[[${#request.getParameter('userName')}]]";
  let adminId =  "[[${#request.getParameter('userId')}]]";

  $(function () {
    $.ajax({
      type:"post",
      url:"/access/roles",
      dataType:"json",
      success:function (data) {
        if(data.code === 200){

          let content = "";

          content += '';

          $(data.data).each(function () {
            content += '<div style="margin-left: 20px" class="layui-form-item">\n' +
                    '        <input type="checkbox" name="roles" value="'+ this.roleCode +'" id="'+ this.roleCode +'" />'+ this.roleName+'\n' +
                    '      </div>\n';
          });

          content += '      <div class="layui-form-item">\n' +
                  '        <button style="margin-left: 80px" class="layui-btn"  lay-submit="" lay-filter="formDemo" id="search" ><i class="layui-icon">确认更改</i></button>\n' +
                  //'        <button class="layui-btn"  lay-submit=""  id="search" onclick="changeRole(this,this.id)"><i class="layui-icon">&#xe615;</i></button>\n' +
                  '      </div>\n';

          let target = $("#form").append(content);
          layui.form.render("checkbox");

        }
      }
    })

    let admin = {};
    admin.adminName = adminName;
    admin.adminId = adminId;

    $.ajax({
      type:"post",
      url:"/access/user-role/name",
      dataType:"json",
      data:admin,
      success:function (data){
        console.log(data);
        $(data.data).each(function(){
          $("#"+this.roleCode).attr("checked","checked");
        })

        layui.form.render("checkbox");
      }
    })

  })


  layui.use(['form','layer'], function(){
    $ = layui.jquery;
    let layer = layui.layer;
    let form = layui.form;

    form.on('submit(formDemo)', function(data){

      let arr = "";
      $("input:checkbox[name='roles']:checked").each(function(i){
        arr = arr+$(this).val();
        arr = arr + ","
      });
      arr=arr.substr(0,arr.lastIndexOf(","))

      let changeInfo = {};
      changeInfo.adminName = adminName;
      changeInfo.adminId = adminId;
      changeInfo.roleStr = arr;
      console.log(changeInfo);
      
      $.ajax({
        type:"post",
        url:"/access/change",
        data: changeInfo,
        dataType:"json",
        success:function (data) {
          console.log(data);
          layer.alert(data.msg, {icon: data.code === 200 ? 6 : 5},function () {
            if(data.code === 200){
              // 获得frame索引
              var index = parent.layer.getFrameIndex(window.name);
              //关闭当前frame
              parent.layer.close(index);
              // 刷新父级
              parent.location.replace(parent.location.href);
            }
            if(data.code === 3000){
              // 获得frame索引
              var index = parent.layer.getFrameIndex(window.name);
              //关闭当前frame
              parent.layer.close(index);
              // 刷新父级
              parent.location.replace(parent.location.href);
            }
          });
        }
      })

      return false;
    })
  });




</script>

</body>

